﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SNAIL.WPF_Controls;
using Microsoft.NodeXL;
using Microsoft.NodeXL.Core;
using Microsoft.NodeXL.Layouts;
using Microsoft.NodeXL.Visualization.Wpf;

namespace SNAIL.Interfaces
{
    public partial class SocialGraph : Form
    {
        private List<Classes.Contact> Contacts;
        public SocialGraph(List<Classes.Contact> Contacts)
        {
            InitializeComponent();
            this.Contacts = Contacts;
        }

        private void SocialGraph_Load(object sender, EventArgs e)
        {
            IVertexCollection oVertices = nodeXLGraph1.nodeXLControl1.Graph.Vertices;
            IEdgeCollection oEdges = nodeXLGraph1.nodeXLControl1.Graph.Edges;

            Dictionary<SNAIL.Classes.Contact, IVertex> VertexCollection = new Dictionary<SNAIL.Classes.Contact, IVertex>();
            // Get the graph's vertex collection.

            //            IVertex oVertexSelf = oVertices.Add();
            //            oVertexSelf.SetValue(ReservedMetadataKeys.PerVertexShape,
            //VertexShape.Circle);


            //            oVertexSelf.SetValue(ReservedMetadataKeys.PerVertexLabel, "MYSELF");

            foreach (var item in Contacts.Where(x => x.RatingsScore < 1))
            {
                IVertex oVertexContact = oVertices.Add();

                oVertexContact.SetValue(ReservedMetadataKeys.PerVertexShape,
VertexShape.Circle);

                //oVertexContact.SetValue(ReservedMetadataKeys.PerVertexShape,
                //    VertexShape.Label);

                oVertexContact.SetValue(ReservedMetadataKeys.PerVertexLabel, item.DisplayString);


                VertexCollection.Add(item, oVertexContact);

                //IEdge oEdge1 = oEdges.Add(oVertexContact, oVertexSelf, true);
            }

            foreach (var item in Contacts.Where(x => x.RatingsScore < 1))
            {
                foreach (var connectedcontact in item.ConnectedContacts)
                {
                    if (Contacts.Contains(connectedcontact))
                    {
                        if (Contacts.Contains(connectedcontact))
                        {
                            if (!VertexCollection.ContainsKey(connectedcontact))
                            {
                                IVertex oVertexContact = oVertices.Add();

                                oVertexContact.SetValue(ReservedMetadataKeys.PerVertexShape,
                                    VertexShape.Circle);

                                //oVertexContact.SetValue(ReservedMetadataKeys.PerVertexShape,
                                //    VertexShape.Label);

                                oVertexContact.SetValue(ReservedMetadataKeys.PerVertexLabel, connectedcontact.DisplayString);

                                VertexCollection.Add(connectedcontact, oVertexContact);

                            }

                            IEdge oEdge1 = oEdges.Add(VertexCollection[item], VertexCollection[connectedcontact], true);
                            oEdge1.SetValue(ReservedMetadataKeys.PerColor, Color.Purple);
                        }
                    }
                }
            }

            //// Customize their appearance.

            //oEdge1.SetValue(ReservedMetadataKeys.PerColor,
            //    Color.FromArgb(255, 55, 125, 98));

            //oEdge1.SetValue(ReservedMetadataKeys.PerEdgeWidth, 3F);
            //oEdge1.SetValue(ReservedMetadataKeys.PerEdgeLabel, "This is edge 1");

            //oEdge2.SetValue(ReservedMetadataKeys.PerEdgeWidth, 5F);
            //oEdge2.SetValue(ReservedMetadataKeys.PerEdgeLabel, "This is edge 2");

            //oEdge3.SetValue(ReservedMetadataKeys.PerColor,
            //    Color.FromArgb(255, 0, 255, 0));

            nodeXLGraph1.nodeXLControl1.DrawGraph(true);
        }
    }
}
